/***************************************************************************
 *                                                                         *
 * Copyright (c) 2007 - 2009 Nuvoton Technology Corp. All rights reserved.*
 *                                                                         *
 ***************************************************************************/
 

#ifdef ECOS
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "drv_api.h"
#include "diag.h"
#include "wbio.h"
#include "NUC900_GE.h"
#include "NUC900_reg.h"
#else
#include <stdio.h>
#include "NUC900_GE.h"
#include "wbio.h"
#ifdef _NUC900_
#include "NUC900_reg.h"
#else
#include "reg702.h"
#endif
#endif

#ifdef USE_DUMP_FILE
char DUMP_FOLDER[256];
#endif

#ifdef SAVE_TO_SD
#include "wbfat.h"

char suFullName[128];

void dump_cmodel()
{
  int *dump_file, nCount;
  char fname[64];

#ifdef DEBUG 
  printf("dump_cmodel(): %d\n", GFX_SIZE);
#endif   

  strcpy(fname, CMODEL_DUMP_FILE);
  fsAsciiToUnicode(fname, suFullName, 1);
  dump_file = fsOpenFile(suFullName, NULL, O_CREATE);
  fsWriteFile(dump_file, _ScreenBufferPtr, GFX_SIZE, &nCount);
  fsCloseFile(dump_file);
  
  //printf("exit dump_cmodel()\n");
}


void dump_screen(char *file_name)
{
  int dump_file, nCount;
  char full_file_name[100];

  if (GFX_BPP==8)
  {
    strcpy(full_file_name, DUMP_ROOT_8BPP);
  }
  else if (GFX_BPP==16)
  {
    strcpy(full_file_name, DUMP_ROOT_16BPP);
  }
  else if (GFX_BPP==32)
  {
    strcpy(full_file_name, DUMP_ROOT_32BPP);
  }
  
  strcat(full_file_name, file_name);

#ifdef DEBUG  
  printf("dump_screen(%s): %d\n", full_file_name, GFX_SIZE);
#endif

  fsAsciiToUnicode(full_file_name, suFullName, 1);
#ifdef USE_DUMP_FILE
  
  dump_file = fsOpenFile(suFullName, NULL, O_CREATE);;
  
  if (dump_file!=NULL)
  {   
    fsWriteFile(dump_file, GFX_START_ADDR, GFX_SIZE, &nCount);
    fsCloseFile(dump_file);
  }

#endif /* USE_DUMP_FILE */

#ifdef DEBUG 
  printf("exit dump_screen()\n");
#endif 
}


void dump_memory(UINT32 start_addr, UINT32 stop_addr)
{
#if 0
  FILE *dump_file;
  int dump_len;
  
  //printf("dump_memory(): %lx-%lx\n", start_addr, stop_addr);
  
  dump_len = stop_addr - start_addr + 1;
  
  dump_file = fopen(MEM_DUMP_FILE,"wb");
  fwrite((void *)start_addr, 1, dump_len, dump_file);
  fclose(dump_file);
#endif  
  //printf("exit dump_memory()\n");
}


void dump_register()
{
#if 0
  FILE *dump_file;
  UINT32 start_port, stop_port;
  UINT32 reg_addr, reg_data;
  
  //printf("dump_register()\n");
  
  dump_file = fopen(REG_DUMP_FILE, "w");
  #ifdef DEBUG 
  fprintf(dump_file, "GRAPHICS ENGINE\n");
  #endif
  
  start_port = IO_BASE+REG_2D_GETG;
  stop_port = IO_BASE+REG_2D_GEMC;
  
  for (reg_addr=start_port; reg_addr<=stop_port; reg_addr+=4)
  {
    reg_data = inpw(reg_addr);
    #ifdef DEBUG 
    fprintf(dump_file, "[%8lx] 0x%8lx\n", reg_data, reg_data);
    #endif
  }  
#endif  
  //printf("exit dump_register()\n");
}


#else //SWI FILE acess


void dump_cmodel()
{
  FILE *dump_file;
  
  //printf("dump_cmodel(): %d\n", GFX_SIZE);
  
  dump_file = fopen(CMODEL_DUMP_FILE, "wb");
  fwrite(_ScreenBufferPtr, 1, GFX_SIZE, dump_file);
  fclose(dump_file);
  
  //printf("exit dump_cmodel()\n");
}

void set_dump_path(char *path)
{
	int length;
	length = strlen(path);
	if(length > 255) {
	    #ifdef DEBUG 
		printf("Error..Path is too long\n");
		#endif
		
		return;
	}
	strcpy(DUMP_FOLDER, path);
	//printf("dump folder is %s\n",DUMP_FOLDER);
}
////////////////////////////////////////
//  Function : dump_screen()
//  Description : Dump the contents of display memory to a file
//                  call the set_dump_path to set dumpping path first
//  Parameter : the path of output file, 
////////////////////////////////////////
void dump_screen(char *file_name)
{
  FILE *dump_file;
  char full_file_name[200];

/*
  if (GFX_BPP==8)
  {
    strcpy(full_file_name, DUMP_ROOT_8BPP);
  }
  else if (GFX_BPP==16)
  {
    strcpy(full_file_name, DUMP_ROOT_16BPP);
  }
  else if (GFX_BPP==32)
  {
    strcpy(full_file_name, DUMP_ROOT_32BPP);
  }
*/
  strcpy(full_file_name, DUMP_FOLDER);
  strcat(full_file_name, file_name);
  
  #ifdef DEBUG 
  printf("dump_screen(%s): %d\n", full_file_name, GFX_SIZE);
  #endif
  
#ifdef USE_DUMP_FILE
  
  dump_file = fopen(full_file_name, "wb");
  
  if (dump_file!=NULL)
  {
    fwrite((void *)GFX_START_ADDR, 1, GFX_SIZE, dump_file);
    fclose(dump_file);
  }

#endif /* USE_DUMP_FILE  */
  
  #ifdef DEBUG 
  printf("exit dump_screen()\n");
  #endif
}

void dump_memory(UINT32 start_addr, UINT32 stop_addr, char *file_name)
{
  FILE *dump_file;
  int dump_len;
  char mem_file_name[200];
  
  //printf("dump_memory(): %lx-%lx\n", start_addr, stop_addr);
  
  dump_len = stop_addr - start_addr + 1;
  
  ////////////////////////
  strcpy(mem_file_name, DUMP_FOLDER);
  strcat(mem_file_name, file_name);
  ///////////////////////
  
  dump_file = fopen(mem_file_name, "wb");
  fwrite((void *)start_addr, 1, dump_len, dump_file);
  fclose(dump_file);
  
  //printf("exit dump_memory()\n");
}


void dump_register()
{
  FILE *dump_file;
  UINT32 start_port, stop_port;
  UINT32 reg_addr, reg_data;
  
  //printf("dump_register()\n");
  
  dump_file = fopen(REG_DUMP_FILE, "w");
  #ifdef DEBUG 
  fprintf(dump_file, "GRAPHICS ENGINE\n");
  #endif
  
  /*
  start_port = IO_BASE+REG_2D_GETG;
  stop_port = IO_BASE+REG_2D_GEMC;
  */
  
  start_port = REG_2D_GETG;
  stop_port = REG_2D_GEMC;
  
  for (reg_addr=start_port; reg_addr<=stop_port; reg_addr+=4)
  {
    reg_data = inpw(reg_addr);
    #ifdef DEBUG 
    fprintf(dump_file, "[%8lx] 0x%8lx\n", reg_data, reg_data);
    #endif
  }  
  
  //printf("exit dump_register()\n");
}

#endif //SAVE_TO_SD